//
//  MYGAcademicSchuledDetailVC.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/5/30.
//

import UIKit
import QMUIKit

class MYGAcademicSchuledDetailVC: MYGBaseController {

    private var model: MYGAcademicForeignModel?
    private var contents:[MYGAcademicForeignCellModel] = []
    
    var id : String = ""
    override func viewDidLoad() {
        super.viewDidLoad()

        configUI()
        getData()
    }
    
    func configUI(){
        title = "日程详情"
        view.backgroundColor = .white
        view.addSubview(headerView)
        headerView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(250)
        }
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(headerView.snp.bottom)
            make.bottom.equalToSuperview()
        }
        
        view.addSubview(liveBtn)
        liveBtn.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.width.equalTo(83.5)
            make.height.equalTo(31.5)
            make.top.equalTo(330)
        }
        
    }
   
    private lazy var headerView: MYGAcademicSchuledDetailHeaderView = {
        let view = MYGAcademicSchuledDetailHeaderView()
        
        return view
    }()
  
    override var hideNavigationController: Bool {
        true
    }
    
    private lazy var tableView: UITableView = {
        let tableV = UITableView()
        tableV.backgroundColor = .rgba(245, 245, 245, 1)
        tableV.delegate = self
        tableV.dataSource = self
        tableV.separatorStyle = .none
        tableV.showsVerticalScrollIndicator = false
        tableV.register(cellWithClass: MYGAcademicSchuledDetailCell.self)
        return tableV
    }()
    
    private lazy var liveBtn: QMUIButton = {
        let btn = QMUIButton()
        btn.setTitle("进入直播间", for: .normal)
        btn.setTitleColor(.rgba(255, 255, 255, 1), for: .normal)
        btn.setBackgroundColor(color: .rgba(10, 53, 167, 1), forState: .normal)
        btn.titleLabel?.font = .regular(14)
        btn.layer.cornerRadius = 5
        btn.layer.masksToBounds = true
        btn.layer.maskedCorners = [.layerMinXMinYCorner,.layerMinXMaxYCorner]
        btn.contentEdgeInsets = UIEdgeInsets(inset: 5)
//        btn.addTarget(self, action: #selector(clickLiveBtn), for: .touchUpInside)
        return btn
    }()
    
}

extension MYGAcademicSchuledDetailVC {
    
    private func getData(){
        var params :[String:Any] = [:]
        params["id"] = id
        
        MYGLoading.show()
        MMYGNetwork<MYGHomePageAPI,MYGAcademicForeignModel>.request(target: .foreign_detail(param: params)) { [self] result in
            MYGLoading.hide()
            guard let item = result else {
                return
            }
            self.model = item
            headerView.getHeaderData(model: item)
            item.speech.forEach { model in
                
                var names:[String] = []
                model.expert.forEach { obj in
                    names.append(obj.expertCn)
                }
                var name = names.joined(separator: ",")
                
                if model.speechCn.isEmpty {
                    name.append(model.expertRoleCn)
                }else {
                    name = model.speechCn
                }
                
                let size = getStringSize(font: .regular(12), viewSize: CGSize(width: screenWidth - 240 , height: CGFLOAT_MAX), str: name)
                
                if model.speechCn.isEmpty {
                    let item = MYGAcademicForeignCellModel(time: model.speechTime, title: model.expertRoleCn,role: "",isSpeechCn: false, models: model.expert,cellHeight: size.height + 30)
                    self.contents.append(item)
                }else {
                    
                    var names:[String] = []
                    model.expert.forEach { obj in
                        names.append(obj.expertCn)
                    }
                    
                    var height:CGFloat = CGFloat(names.count * 20)
                    if height < size.height {
                        height = size.height
                    }
                    
                    
                    let item = MYGAcademicForeignCellModel(time: model.speechTime, title: model.speechCn,role: model.expertRoleCn,isSpeechCn: true, models: model.expert,cellHeight: height + 30)
                    self.contents.append(item)
                }
            }
            tableView.reloadData()
            
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
    }
    
}

extension MYGAcademicSchuledDetailVC:UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.contents.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let model = self.contents[indexPath.row]
        return model.cellHeight
     }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: MYGAcademicSchuledDetailCell.self)
        cell.model = self.contents[indexPath.row]
        cell.clickNameBlock = {[weak self]id in
            let vc = MYGExpertTaskDetailViewController()
            vc.id = id
            self?.navigationController?.pushViewController(vc)
        }
        return cell
    }
   
}
